package graph.net.internal;


import graph.net.ServerSession;
import graph.service.GraphService;

import java.util.HashMap;
import java.util.Map;



/************************************************
 * @author amahmood
 * 
 *         Maintains a cache of service objects. Command object uses it to
 *         service objects.
 ***********************************************/
public class ServiceLocator
{
    Map<ServiceName, Object> myServices;

    /**************************************************
     * 
     *************************************************/
    public ServiceLocator()
    {
        myServices = new HashMap<ServiceName, Object>();
    }

    /**************************************************
     * @return
     * @throws Exception 
     *************************************************/
    public GraphService getGraphService() throws IllegalStateException
    {
        Object obj = myServices.get(ServiceName.GraphService);

        if (obj == null || !(obj instanceof GraphService))
           throw new IllegalStateException("Unable to find graph service");

        return (GraphService) obj;
    }

    /**************************************************
     * @param serviceId
     * @return
     *************************************************/
    public Object getService(ServiceName serviceId)
    {
        return myServices.get(serviceId);
    }

    /**************************************************
     * @param serviceId
     * @param serviceObject
     *************************************************/
    public void putService(ServiceName serviceId, Object serviceObject)
    {
        myServices.put(serviceId, serviceObject);
    }
    
    
    /***************************************************
     * @return
     * @throws Exception 
     **************************************************/
    public ServerSession getServiceSession() throws IllegalStateException
    {
        Object obj = myServices.get(ServiceName.ServerSession);

        if (obj == null || !(obj instanceof ServerSession))
            throw new IllegalStateException("Unable to find server session service");;

        return (ServerSession) obj;
    }

}
